package com.anolesoft.yr.mbus.base.mapper;

import com.anolesoft.yr.mbus.base.entity.BaseCompanyDO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.LocalDateTimeTypeHandler;
import org.apache.ibatis.type.LocalDateTypeHandler;

import java.util.List;

/**
 * @author zhj
 * @date 2020/8/8 15:55
 */
@Mapper
public interface BaseCompanyMapper {
    @Insert("insert into base_company (" +
            "objuid," +
            "fac_code," +
            "base_region_uid," +
            "company_code," +
            "company_name," +
            "company_other_name," +
            "company_address," +
            "establish_time," +
            "company_website," +
            "email," +
            "contact_person," +
            "contact_number," +
            "fax_number," +
            "company_grade," +
            "legal_person," +
            "company_type," +
            "bank," +
            "account," +
            "tax_number," +
            "c_user_uid," +
            "c_time," +
            "m_user_uid," +
            "m_time," +
            "del_flag," +
            "remark" +
            ") values (" +
            "#{objuid,jdbcType=VARCHAR}," +
            "#{factoryCode,jdbcType=VARCHAR}," +
            "#{baseRegionUid,jdbcType=VARCHAR}," +
            "#{companyCode,jdbcType=VARCHAR}," +
            "#{companyName,jdbcType=VARCHAR}," +
            "#{companyOtherName,jdbcType=VARCHAR}," +
            "#{companyAddress,jdbcType=VARCHAR}," +
            "#{establishTime,jdbcType=DATE}," +
            "#{companyWebsite,jdbcType=VARCHAR}," +
            "#{email,jdbcType=VARCHAR}," +
            "#{contactPerson,jdbcType=VARCHAR}," +
            "#{contactNumber,jdbcType=VARCHAR}," +
            "#{faxNumber,jdbcType=VARCHAR}," +
            "#{companyGrade,jdbcType=VARCHAR}," +
            "#{legalPerson,jdbcType=VARCHAR}," +
            "#{companyType,jdbcType=VARCHAR}," +
            "#{bank,jdbcType=VARCHAR}," +
            "#{account,jdbcType=VARCHAR}," +
            "#{taxNumber,jdbcType=VARCHAR}," +
            "#{createUserUid,jdbcType=VARCHAR}," +
            "now()," +
            "#{modifyUserUid,jdbcType=VARCHAR}," +
            "now()," +
            "0," +
            "#{remark,jdbcType=VARCHAR}" +
            ")")
    int insert(BaseCompanyDO baseCompanyDO);

    @Update("update base_company set " +
            "fac_code = #{factoryCode,jdbcType=VARCHAR}," +
            "base_region_uid = #{baseRegionUid,jdbcType=VARCHAR}," +
            "company_code = #{companyCode,jdbcType=VARCHAR}," +
            "company_name = #{companyName,jdbcType=VARCHAR}," +
            "company_other_name = #{companyOtherName,jdbcType=VARCHAR}," +
            "company_address = #{companyAddress,jdbcType=VARCHAR}," +
            "establish_time = #{establishTime,jdbcType=DATE}," +
            "company_website = #{companyWebsite,jdbcType=VARCHAR}," +
            "email = #{email,jdbcType=VARCHAR}," +
            "contact_person = #{contactPerson,jdbcType=VARCHAR}," +
            "contact_number = #{contactNumber,jdbcType=VARCHAR}," +
            "fax_number = #{faxNumber,jdbcType=VARCHAR}," +
            "company_grade = #{companyGrade,jdbcType=VARCHAR}," +
            "legal_person = #{legalPerson,jdbcType=VARCHAR}," +
            "company_type = #{companyType,jdbcType=VARCHAR}," +
            "bank = #{bank,jdbcType=VARCHAR}," +
            "account = #{account,jdbcType=VARCHAR}," +
            "tax_number = #{taxNumber,jdbcType=VARCHAR}," +
            "m_user_uid = #{modifyUserUid,jdbcType=VARCHAR}," +
            "m_time = now()," +
            "remark = #{remark,jdbcType=VARCHAR}" +
            " where objuid = #{objuid,jdbcType=VARCHAR}")
    int update(BaseCompanyDO baseCompanyDO);

    @Update("update base_company set del_flag = 1 where objuid = #{id,jdbcType=VARCHAR}")
    int delete(@Param("id") String id);

    @Results(id = "BaseCompanyResultMap", value = {
            @Result(id = true, column = "objuid", property = "objuid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "fac_code", property = "factoryCode", jdbcType = JdbcType.VARCHAR),
            @Result(column = "base_region_uid", property = "baseRegionUid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "company_code", property = "companyCode", jdbcType = JdbcType.VARCHAR),
            @Result(column = "company_name", property = "companyName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "company_other_name", property = "companyOtherName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "company_address", property = "companyAddress", jdbcType = JdbcType.VARCHAR),
            @Result(column = "establish_time", property = "establishTime", typeHandler = LocalDateTypeHandler.class),
            @Result(column = "company_website", property = "companyWebsite", jdbcType = JdbcType.VARCHAR),
            @Result(column = "email", property = "email", jdbcType = JdbcType.VARCHAR),
            @Result(column = "contact_person", property = "contactPerson", jdbcType = JdbcType.VARCHAR),
            @Result(column = "contact_number", property = "contactNumber", jdbcType = JdbcType.VARCHAR),
            @Result(column = "fax_number", property = "faxNumber", jdbcType = JdbcType.VARCHAR),
            @Result(column = "company_grade", property = "companyGrade", jdbcType = JdbcType.VARCHAR),
            @Result(column = "legal_person", property = "legalPerson", jdbcType = JdbcType.VARCHAR),
            @Result(column = "company_type", property = "companyType", jdbcType = JdbcType.VARCHAR),
            @Result(column = "bank", property = "bank", jdbcType = JdbcType.VARCHAR),
            @Result(column = "account", property = "account", jdbcType = JdbcType.VARCHAR),
            @Result(column = "tax_number", property = "taxNumber", jdbcType = JdbcType.VARCHAR),
            @Result(column = "c_user_uid", property = "createUserUid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "c_time", property = "createTime", typeHandler = LocalDateTimeTypeHandler.class),
            @Result(column = "m_user_uid", property = "modifyUserUid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "m_time", property = "modifyTime", typeHandler = LocalDateTimeTypeHandler.class),
            @Result(column = "del_flag", property = "deleteFlag", jdbcType = JdbcType.BOOLEAN),
            @Result(column = "remark", property = "remark", jdbcType = JdbcType.VARCHAR),
            @Result(column = "base_region_uid", property = "region", one = @One(select = "com.anolesoft.yr.mbus.base.mapper.BaseRegionMapper.selectOneById", fetchType = FetchType.EAGER)),
            @Result(column = "objuid", property = "contacts", many = @Many(select = "com.anolesoft.yr.mbus.base.mapper.BaseContactMapper.selectListByTargetUid", fetchType = FetchType.DEFAULT))
    })
    @Select("select " +
            "objuid, fac_code, base_region_uid, company_code, company_name," +
            "company_other_name, company_address, establish_time, company_website," +
            "email, contact_person, contact_number, fax_number, company_grade," +
            "legal_person, company_type, bank, account, tax_number," +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark " +
            "from base_company where del_flag =0 ")
    List<BaseCompanyDO> selectAll();

    @ResultMap("BaseCompanyResultMap")
    @Select("select " +
            "objuid, fac_code, base_region_uid, company_code, company_name," +
            "company_other_name, company_address, establish_time, company_website," +
            "email, contact_person, contact_number, fax_number, company_grade," +
            "legal_person, company_type, bank, account, tax_number," +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark " +
            "from base_company where del_flag =0 ")
    List<BaseCompanyDO> selectPageAll(Page<BaseCompanyDO> page);

    @ResultMap("BaseCompanyResultMap")
    @Select("select " +
            "objuid, fac_code, base_region_uid, company_code, company_name," +
            "company_other_name, company_address, establish_time, company_website," +
            "email, contact_person, contact_number, fax_number, company_grade," +
            "legal_person, company_type, bank, account, tax_number," +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark " +
            "from base_company where del_flag =0 and objuid = #{id,jdbcType=VARCHAR}")
    BaseCompanyDO selectOneById(@Param("id") String id);


    @ResultMap("BaseCompanyResultMap")
    @Select("<script>" +
            "select " +
            "objuid, fac_code, base_region_uid, company_code, company_name," +
            "company_other_name, company_address, establish_time, company_website," +
            "email, contact_person, contact_number, fax_number, company_grade," +
            "legal_person, company_type, bank, account, tax_number," +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark " +
            "from base_company where del_flag =0 " +
            "<if test =\"o.factoryCode !=null and o.factoryCode != '' \">" +
            "   and fac_code = #{o.factoryCode, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.baseRegionUid !=null and o.baseRegionUid != '' \">" +
            "   and base_region_uid = #{o.baseRegionUid, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.companyCode !=null and o.companyCode != '' \">" +
            "   and company_code like #{o.companyCode, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.companyName !=null and o.companyName != '' \">" +
            "   and company_name like #{o.companyName, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.companyOtherName !=null and o.companyOtherName != '' \">" +
            "   and company_other_name like #{o.companyOtherName, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.companyAddress !=null and o.companyAddress != '' \">" +
            "   and company_address like #{o.companyAddress, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.objuid !=null and o.objuid != '' \">" +
            "   and objuid = #{o.objuid, jdbcType=VARCHAR} " +
            "</if>" +
            "</script>")
    List<BaseCompanyDO> selectList(@Param("o") BaseCompanyDO baseCompanyDO);

    @ResultMap("BaseCompanyResultMap")
    @Select("<script>" +
            "select " +
            "objuid, fac_code, base_region_uid, company_code, company_name," +
            "company_other_name, company_address, establish_time, company_website," +
            "email, contact_person, contact_number, fax_number, company_grade," +
            "legal_person, company_type, bank, account, tax_number," +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark " +
            "from base_company where del_flag =0 " +
            "<if test =\"o.factoryCode !=null and o.factoryCode != '' \">" +
            "   and fac_code = #{o.factoryCode, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.baseRegionUid !=null and o.baseRegionUid != '' \">" +
            "   and base_region_uid = #{o.baseRegionUid, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.companyCode !=null and o.companyCode != '' \">" +
            "   and company_code like #{o.companyCode, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.companyName !=null and o.companyName != '' \">" +
            "   and company_name like #{o.companyName, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.companyOtherName !=null and o.companyOtherName != '' \">" +
            "   and company_other_name like #{o.companyOtherName, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.companyAddress !=null and o.companyAddress != '' \">" +
            "   and company_address like #{o.companyAddress, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.objuid !=null and o.objuid != '' \">" +
            "   and objuid = #{o.objuid, jdbcType=VARCHAR} " +
            "</if>" +
            "</script>")
    List<BaseCompanyDO> selectPageList(@Param("o") BaseCompanyDO baseCompanyDO, Page<BaseCompanyDO> page);

    @Select("select count(objuid) from base_company where del_flag = 0 and company_code = #{companyCode, jdbcType=VARCHAR}")
    Integer checkUniqueForCompanyCode(@Param("companyCode") String companyCode);

    @Select("select count(objuid) from base_company where del_flag = 0 and company_name = #{companyName, jdbcType=VARCHAR}")
    Integer checkUniqueForCompanyName(@Param("companyName") String companyName);

    @Select("select count(objuid) from base_company where del_flag = 0 and company_code = #{companyCode, jdbcType=VARCHAR} and objuid != #{id, jdbcType=VARCHAR}")
    Integer checkUniqueForCompanyCodeUpdate(@Param("companyCode") String companyCode, @Param("id") String id);

    @Select("select count(objuid) from base_company where del_flag = 0 and company_name = #{companyName, jdbcType=VARCHAR} and objuid != #{id, jdbcType=VARCHAR}")
    Integer checkUniqueForCompanyNameUpdate(@Param("companyName") String companyName, @Param("id") String id);
}
